package io.xccit.race.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import io.xccit.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.xccit.common.annotation.Log;
import io.xccit.common.core.controller.BaseController;
import io.xccit.common.core.domain.AjaxResult;
import io.xccit.common.enums.BusinessType;
import io.xccit.race.domain.RaceCompetitionSignup;
import io.xccit.race.service.IRaceCompetitionSignupService;
import io.xccit.common.utils.poi.ExcelUtil;
import io.xccit.common.core.page.TableDataInfo;

/**
 * 竞赛报名信息Controller
 * 
 * @author ywx
 * @date 2025-10-10
 */
@RestController
@RequestMapping("/race/competitionSignup")
public class RaceCompetitionSignupController extends BaseController
{
    @Autowired
    private IRaceCompetitionSignupService raceCompetitionSignupService;

    /**
     * 查询竞赛报名信息列表
     */
    @PreAuthorize("@ss.hasPermi('race:competitionSignup:list')")
    @GetMapping("/list")
    public TableDataInfo list(RaceCompetitionSignup raceCompetitionSignup)
    {
        startPage();
        List<RaceCompetitionSignup> list = raceCompetitionSignupService.selectRaceCompetitionSignupList(raceCompetitionSignup);
        return getDataTable(list);
    }

    /**
     * 导出竞赛报名信息列表
     */
    @PreAuthorize("@ss.hasPermi('race:competitionSignup:export')")
    @Log(title = "竞赛报名信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RaceCompetitionSignup raceCompetitionSignup)
    {
        List<RaceCompetitionSignup> list = raceCompetitionSignupService.selectRaceCompetitionSignupList(raceCompetitionSignup);
        ExcelUtil<RaceCompetitionSignup> util = new ExcelUtil<RaceCompetitionSignup>(RaceCompetitionSignup.class);
        util.exportExcel(response, list, "竞赛报名信息数据");
    }

    /**
     * 检查手机号是否已报名指定竞赛
     */
    @GetMapping("/checkPhone/{competitionId}/{phone}")
    public AjaxResult checkPhoneRegistration(@PathVariable Long competitionId, @PathVariable String phone)
    {
        boolean isRegistered = raceCompetitionSignupService.checkPhoneRegistration(competitionId, phone);
        return success(isRegistered);
    }

    /**
     * 检查学号报名冲突（同一天竞赛时间冲突）
     */
    @GetMapping("/checkConflict/{competitionId}/{studentId}")
    public AjaxResult checkRegistrationConflict(@PathVariable Long competitionId, @PathVariable String studentId)
    {
        boolean hasConflict = raceCompetitionSignupService.checkRegistrationConflict(competitionId, studentId);
        return success(hasConflict);
    }

    /**
     * 根据当前登录用户手机号查询报名详情
     */
    @GetMapping("/myRegistrations")
    public AjaxResult getMyRegistrations()
    {
        List<RaceCompetitionSignup> registrations = raceCompetitionSignupService.getRegistrationsByCurrentUserPhone();
        Map<String, Object> result = new HashMap<>();
        result.put("registrations", registrations);
        result.put("count", registrations.size());
        return success(result);
    }

    /**
     * 获取竞赛报名信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('race:competitionSignup:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(raceCompetitionSignupService.selectRaceCompetitionSignupById(id));
    }

    /**
     * 新增竞赛报名信息
     */
    @PreAuthorize("@ss.hasPermi('race:competitionSignup:add')")
    @Log(title = "竞赛报名信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RaceCompetitionSignup raceCompetitionSignup)
    {
        // 检查是否重复报名
        if (raceCompetitionSignupService.checkDuplicateSignup(raceCompetitionSignup)) {
            return error("该用户已报名此竞赛，请勿重复报名");
        }
        return toAjax(raceCompetitionSignupService.insertRaceCompetitionSignup(raceCompetitionSignup));
    }

    /**
     * 修改竞赛报名信息
     */
    @PreAuthorize("@ss.hasPermi('race:competitionSignup:edit')")
    @Log(title = "竞赛报名信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RaceCompetitionSignup raceCompetitionSignup)
    {
        return toAjax(raceCompetitionSignupService.updateRaceCompetitionSignup(raceCompetitionSignup));
    }

    /**
     * 删除竞赛报名信息
     */
    @PreAuthorize("@ss.hasPermi('race:competitionSignup:remove')")
    @Log(title = "竞赛报名信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(raceCompetitionSignupService.deleteRaceCompetitionSignupByIds(ids));
    }

    /**
     * 根据竞赛ID查询报名信息
     */
    @GetMapping("/competition/{competitionId}")
    public AjaxResult getSignupByCompetitionId(@PathVariable("competitionId") Long competitionId)
    {
        List<RaceCompetitionSignup> list = raceCompetitionSignupService.selectRaceCompetitionSignupByCompetitionId(competitionId);
        return success(list);
    }

    /**
     * 根据姓名查询报名信息
     */
    @GetMapping("/name/{participantName}")
    public AjaxResult getSignupByName(@PathVariable("participantName") String participantName)
    {
        List<RaceCompetitionSignup> list = raceCompetitionSignupService.selectRaceCompetitionSignupByName(participantName);
        return success(list);
    }

    /**
     * 根据学校查询报名信息
     */
    @GetMapping("/school/{schoolName}")
    public AjaxResult getSignupBySchool(@PathVariable("schoolName") String schoolName)
    {
        List<RaceCompetitionSignup> list = raceCompetitionSignupService.selectRaceCompetitionSignupBySchool(schoolName);
        return success(list);
    }

    /**
     * 获取报名统计信息
     */
    @GetMapping("/statistics/{competitionId}")
    public AjaxResult getSignupStatistics(@PathVariable("competitionId") Long competitionId)
    {
        Map<String, Object> statistics = raceCompetitionSignupService.getSignupStatistics(competitionId);
        return success(statistics);
    }

    /**
     * 审核报名信息
     */
    @PreAuthorize("@ss.hasPermi('race:competitionSignup:audit')")
    @Log(title = "审核报名信息", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/{id}")
    public AjaxResult auditSignup(@PathVariable("id") Long id, @RequestBody Map<String, Object> params)
    {
        Integer status = (Integer) params.get("status");
        String auditRemark = (String) params.get("auditRemark");
        return toAjax(raceCompetitionSignupService.auditSignup(id, status, auditRemark));
    }

    /**
     * 检查报名是否重复
     */
    @PostMapping("/checkDuplicate")
    public AjaxResult checkDuplicate(@RequestBody RaceCompetitionSignup raceCompetitionSignup)
    {
        boolean isDuplicate = raceCompetitionSignupService.checkDuplicateSignup(raceCompetitionSignup);
        return success(isDuplicate);
    }

    /**
     * 获取当前用户的报名列表
     */
    @GetMapping("/my")
    public TableDataInfo getMyRegistrations(RaceCompetitionSignup raceCompetitionSignup)
    {
        startPage();
        // 设置当前用户ID
        LoginUser loginUser = getLoginUser();
        raceCompetitionSignup.setUserId(loginUser.getUserId());
        List<RaceCompetitionSignup> list = raceCompetitionSignupService.selectRaceCompetitionSignupList(raceCompetitionSignup);
        return getDataTable(list);
    }

    /**
     * 取消报名
     */
    @PutMapping("/cancel/{id}")
    public AjaxResult cancelRegistration(@PathVariable("id") Long id)
    {
        LoginUser loginUser = getLoginUser();
        return toAjax(raceCompetitionSignupService.cancelSignup(id, loginUser.getUserId()));
    }
}
